<template>
  <div class="login-wrapper">
    <div class="login-content">
      <div class="logo-info">
        <img src="../assets/logo.png" />
        <p>Vue3 Admin Project</p>
        <span>Vue3 Admin Project 是一款中/后台管理系统</span>
      </div>
      <el-form ref="loginFormRef" @toLogin.prevent :model="loginForm" :rules="loginRules" hide-required-asterisk>
        <el-form-item prop="username">
          <el-input prefix-icon="User" size="default" v-model="loginForm.username" placeholder="请输入用户名">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="Lock" size="default" v-model="loginForm.password" placeholder="请输入密码" show-password
            @keyup.enter="toLogin(loginFormRef)">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="default" :loading="loading" style="width: 100%" type="primary"
            @click="toLogin(loginFormRef)">登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="other-login">
        <el-button text disabled>其它登录方式</el-button>
        <el-button text disabled>忘记密码</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";

const router = useRouter();

const loading = ref(false);
const loginFormRef = ref(null);

const loginForm = reactive({
  username: "admin",
  password: "123456",
});

const loginRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
})

const toLogin = async (formEl) => {
  //登陆
  if (!formEl) {
    return;
  }
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log(loginForm.username)
      console.log(loginForm.password)
      loading.value = true;
      const userObj = JSON.stringify({
        userName: loginForm.username,
        passWord: loginForm.password,
      })
      window.sessionStorage.setItem('user', userObj)
      router.push({ path: "/home" });
      loading.value = false;
    } else {
      loading.value = false;
      console.log('error submit!!')
      return false
    }
  });
};

</script>

<style lang="scss" scoped>
.login-wrapper {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url(../assets/login_bg.png);

  .login-content {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    overflow: hidden;
    padding: 0 26px 24px 26px;
    box-sizing: border-box;
    margin-left: -198px;
    margin-top: -255px;

    .logo-info {
      padding: 12px;
      text-align: center;

      img {
        height: 55px;
      }

      p {
        font-size: 24px;
        padding: 2px 0px;
      }

      span {
        font-size: 14px;
        color: #909399;
      }
    }

    .other-login {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>